Applied Methods · MA Public Administration
2026-10-02
Basics of Quarto documents
From descriptive statistics to inference
Normal distribution
Central limit theorem
Univariate inference
Bivariate statistics
Linear regressions
In a normal workflow, you may have separate files:
This can become difficult to update.
With Quarto, the analysis and the written explanation are kept together.
We will start from a very small document and progressively add the pieces needed for a clean report.
A Quarto document is a plain-text file that can become a formatted document.
A Quarto document combines three ingredients.
| Ingredient | What it controls | Example |
|---|---|---|
| YAML header | document settings | title, author, output format |
| Markdown | written text | headings, lists, links |
| R chunks | analysis | tables, models, figures |
This is enough to create a real HTML document.
In RStudio, you can create a new Quarto document with:
File → New File → Quarto Document
Then choose:
Create a Quarto document
Go in RStudio and create a qmd file with:
File → New File → Quarto Document
Give it a title, an author (you) and a .html output format
| Button | What it does |
|---|---|
| Run | Runs selected R code or a code chunk |
| Render | Creates the final document |
Running code in the console is not the same as rendering the whole document.
A document may work line by line but fail when rendered.
Common reasons:
.qmd file,A good Quarto document should run from top to bottom by itself.
The YAML header is the part between the two --- lines.
It tells Quarto how to create the document.
| Field | Role |
|---|---|
title |
title of the document |
subtitle |
optional subtitle |
author |
author name |
date |
date shown in the document |
format |
output type |
execute |
global code options |
YAML uses indentation to show that an option belongs to another option.
Read this as:
For the HTML format, create a table of contents.
The same .qmd file can produce different outputs.
For this course, HTML is usually the safest and most flexible format.
Change the YAML header of your Qurto document to include
Markdown is a simple way to write formatted text.
You write plain text with a few simple symbols.
Use headings to organise your report.
Good reports are structured before they are detailed.
Or numbered lists:
A link:
An image:
With a size:
Do not only show outputs.
Explain what the reader is about to see.
In the next section, we describe the distribution of the outcome variable. This helps us understand the scale of the variable before estimating the model.
In your Quarto document add:
Everything inside the chunk is ordinary R code.
A setup chunk is usually placed at the beginning of the document.
Load packages at the beginning of the document.
Chunk options control how the code and output appear.
| Option | Question it answers |
|---|---|
echo |
Should the code be shown? |
warning |
Should warnings be shown? |
message |
Should messages be shown? |
eval |
Should the code be run? |
Useful when the reader needs the result but not the code.
Do not hide warnings because you want to ignore errors. Hide them only when they are not useful for the reader.
If you want the same option for all chunks, put it in the YAML header.
This avoids repeating the same options in every chunk.
Quarto automatically includes the figure in the document.
Use this only when the default size does not work well.
A simple data frame will be printed as a table.
For nicer tables, you can use packages such as gt, knitr, or modelsummary.
The important idea is simple:
Quarto displays the output produced by R.
Create three R chunck with the code
For your data driven report, a simple structure is enough.
Quarto can also handle:
Inline equation:
Display equation:
A citation usually requires a bibliography file.
Then cite in the text:
Useful official resources:
The skill is not memorising options. The skill is knowing what to search for.
A Quarto document has three main ingredients:
| Ingredient | Role |
|---|---|
| YAML | document settings |
| Markdown | written explanation |
| R chunks | analysis and output |
Quarto is not mainly about formatting. It is about connecting analysis and communication.
Descriptive statistics describe what we have.
Inference aims to generalize from what we have to what we do not have.
Although individual events are random, collectively we observe some patterns
If events are random, they follow a normal distribution.
What is the probability that given what we see, the events are not random?
Never 100% sure of anything.
Some threshold: 95%, 99%, 99.9%
From the bell curve, how many SD do we need to take to be sure at this level?
Even if the distribution of a variable is not normal, we reach the normal distribution if we sample the distribution and measure the distributions of means.
\[\bar{x} = N(\mu, \frac{\sigma^2}{n})\]
Descrptively, a mean of a sample is
\[\bar{x}=\frac{1}{n}\sum_{i=1}^n x_i\]
With the CLT, we know that our observed mean is normally distributed on the true mean
The mean of a sample is an objective value.
We cannot really say something general about it.
To do so, we need to quantify the uncertainty of what we observe.
We can use the standard deviation to quantify the uncertainty and provide leels of confidence.
Metric X metric : Correlation
Categorical X categorical : Chi-Square test
Metric X Categorical : ANOVA and t-test
Hypothesis testing: Tests that enables us to verify/ falsify hypothesis
Correlation –> H0: These variables are not correlated
Chi-Squared test –> H0: The distribution of these variable are independent
Anova/T-test –> H0: There are no difference between the groups
Definition: Measures the strength and direction of the linear relationship between two numerical variables.
Interpretation: The correlation coefficient (r) ranges from -1 to +1, with 0 indicating no linear relationship.
Pearson's product-moment correlation
data: x and y
t = -65.343, df = 998, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.9114254 -0.8878619
sample estimates:
cor
-0.9003012
Definition: Tests whether two categorical variables are statistically independent.
Interpretation: A significant p-value (p < 0.05) suggests an association between the two variables.
gender
pets Men Women
Cat 534 799
Dog 570 803
Fish 92 148
Snake 23 31
Pearson's Chi-squared test
data: table(pets, gender)
X-squared = 1.2362, df = 3, p-value = 0.7443
Definition: Tests whether the means of two groups are statistically different.
Interpretation: A significant p-value (p < 0.05) suggests that the population means of the two groups differ.
Welch Two Sample t-test
data: value by group
t = -22.019, df = 1993.9, p-value < 2.2e-16
alternative hypothesis: true difference in means between group A and group B is not equal to 0
95 percent confidence interval:
-1.0791526 -0.9026429
sample estimates:
mean in group A mean in group B
4.974658 5.965556
Definition: Tests whether the means of three or more groups are equal.
Interpretation: A significant p-value (p < 0.05) suggests that at least one group mean differs from the others.
Regressions are a statistical method used to examine the relationship between a dependent variable (Y) and one or more independent variables (X).
They estimate the direction and the strength of an association.
They enable to do statistical inference and quantify the uncertainty
They are used to estimate effect and to predict outcomes.
The most basic equatin for a regression is:
\[ Y_i = \beta_0 + \beta_{1}X_i + \epsilon_i\]
In R, we can estimate the effect of X on y with :
Call:
lm(formula = y ~ x, data = data_test)
Residuals:
Min 1Q Median 3Q Max
-43.836 -9.264 -0.215 9.322 39.888
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 6.09084 0.84297 7.225 9.91e-13 ***
x 0.87392 0.01433 60.972 < 2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 13.81 on 998 degrees of freedom
Multiple R-squared: 0.7884, Adjusted R-squared: 0.7881
F-statistic: 3718 on 1 and 998 DF, p-value: < 2.2e-16
Each observation has an error associated
\[Y_i = \beta_0 + \sum{\beta_{n}X_{n,i}}\]
Or,
\[Y_i = \beta_0 + \beta_{1}X_{1,i} + \beta_{2}X_{2,i} + ... + \beta_{n}X_{n,i}\] Regression can have as many covariate as possible.
This enables us to control for covariates.
Why do we use control variables in regression?
Cofounders
Ommitted variables
Improving precision
Testing the robustness
We have 2 variables that are distributed as follows
We can see a positive association between the two variables.
Pearson's product-moment correlation
data: x and y
t = 39.226, df = 2998, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.5582293 0.6055476
sample estimates:
cor
0.5823816
However, a second variable x2 separates the variables into three distinct groups
The effect observed between groups can be different than the effect observed within groups.
To avoid this, we need to control for variables that have a potential effect on x (IV) and y (DV).
Regression coefficients (positive/negative? Significant?)
Constant (When X=0, what is the predicted value of y?)
R Squared (Explanatory power of the model)
Fisher test, degrees of Freedom, distribution of error
Estimate the effect of the independent variable on the dependent variable
When X increases, how much does Y change?
\[Y_i = \beta_0 + \beta_{1}X_{1,i} + \beta_{2}X_{2,i} + ... + \beta_{n}X_{n,i}\] In the equation, the \(\beta_n\) that multiply variables
When the matrix of covariates is equal to 0, what is the predicted value of Y?
Predictive quality o our model
Share of the dependent variable variance explained by the model
Between 0 and 1, where 0 mean nothing is explained and 1 mean no variance left to explain.
Indicated whether the errors are normally distributed.
Ideally, we would like our model to have as much positive and negative error (normality assumption)
We would also want the error to be distributed equally across all values of X and Y (Heterosedasticity asusmption)
Test whether the model is statistically sound
Whether testing this model is better than not testing it (appr. better than random guess)
Tells us how much degree of freedom we have in the model.
Impossible to include more variables than observations
Ideally, we would like to have models with 10 times more observation than variables.
Call:
lm(formula = y ~ x, data = data_test)
Residuals:
Min 1Q Median 3Q Max
-43.836 -9.264 -0.215 9.322 39.888
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 6.09084 0.84297 7.225 9.91e-13 ***
x 0.87392 0.01433 60.972 < 2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 13.81 on 998 degrees of freedom
Multiple R-squared: 0.7884, Adjusted R-squared: 0.7881
F-statistic: 3718 on 1 and 998 DF, p-value: < 2.2e-16
Tables
Effect plots
Predictions
===============================================
Dependent variable:
---------------------------
y
-----------------------------------------------
x 0.874***
(0.014)
Constant 6.091***
(0.843)
-----------------------------------------------
Observations 1,000
R2 0.788
Adjusted R2 0.788
Residual Std. Error 13.814 (df = 998)
F Statistic 3,717.567*** (df = 1; 998)
===============================================
Note: *p<0.1; **p<0.05; ***p<0.01
## Extract coeficients
coefs_mod <- data.frame(coef = summary(mod)[["coefficients"]][2,1],
sd = summary(mod)[["coefficients"]][2,2])
ggplot()+
geom_point(aes(x=coefs_mod$coef, y="X"))+
geom_errorbarh(aes(y="X",
xmin = coefs_mod$coef - coefs_mod$sd*1.96, ## 95%
xmax = coefs_mod$coef +coefs_mod$sd*1.96), height=.02)+
geom_vline(xintercept = 0)In the folder replication, open the file Replication Kübler et al. (2025) - Exercice
Your task is to reproduce some of the analyses presented in the paper.
Join in groups
Create a Quarto document with your names, and title of your project
Create the general structure of your report
Try to import your data and do some first analyses
Try to conduct a meaningful regression with your data for your project.